import {Component, Input, OnChanges, OnDestroy, OnInit} from '@angular/core';
import {TipService} from "../../main/services/tip.service";

@Component({
  selector: 'app-tip',
  templateUrl: './tip.component.html',
  styleUrls: ['./tip.component.css']
})
export class TipComponent implements OnInit, OnDestroy {

  show: boolean = false;
  msg: string = '';
  timeoutId = null;
  @Input() type;

  constructor(private tipService: TipService) {

  }

  ngOnInit() {
    this.tipService.tipEmit.subscribe(res => {
      if (this.show) return;
      this.msg = res.message;
      this.show = res.flag;
      if (this.timeoutId) {
        clearTimeout(this.timeoutId);
      }
      this.timeoutId = setTimeout(() => {
        this.show = false;
        this.msg = '';
      }, 2000)
    });
  }

  ngOnDestroy() {

    if(this.timeoutId) {
      clearTimeout(this.timeoutId);
    }
  }

}
